<?php
declare (strict_types=1);

namespace app\common\model;

use app\common\facade\Jpush;
use think\facade\Event;

class News extends Base
{
    protected $name = 'news';

    public function getPositionAttr($value)
    {
        return newsPosition($value);
    }

    public function getTypeAttr($value)
    {
        return newsType($value);
    }

    public function getIsPushAttr($value): string
    {
        return $value == 1 ? '是' : '否';
    }

    public function getContentAttr($value)
    {
        return $value ? htmlspecialchars_decode($value) : '';
    }

    public static function recordLog($model)
    {

        $emp_id = session('admin_id');
        $emp_name = Admin::where('id', $emp_id)->value('admin_name');
        $title = '新闻公告';
        $operation = getOperation($model->operation);
        if(in_array($model->operation,['updateAll','deleteAll'])){
            $msg = $operation .'【' . $title . '】'. '标识ID【' . implode(',',$model->ids) . '】';
            Logs::adds($msg, $emp_id, $emp_name);
        }else{
            $msg = $operation .'【' . $title . '】'. '标识ID【' . $model->id . '】标题【' . $model->title . '】';
            Logs::adds($msg, $emp_id, $emp_name);
        }
    }

    public static function _all($map, $param, $hasOne, $like = [], $order = 'create_time,desc', $append = [], $filed = '*', $export = false): array
    {
        try {
            $page = $param['page'] ?? 1;
            $limit = $param['limit'] ?? 10;

            !$order && $order = 'create_time,desc';

            if(!isset($map['is_del'])){
                $map['is_del'] = 0;
            }
            $position = null;
            if(isset($map['position'])){
                $position = $map['position'];
                unset($map['position']);
            }
            $that = parent::where($map);

            if($position == 1) $that = $that->whereIn('position', [1,3]);
            else $that = $that->where('position',2);

            if ($hasOne) {
                $that = $that->with($hasOne);
            }

            if (isset($param['times']) && $param['times']) {
                $times = explode(' - ', $param['times']);
                $that->whereBetweenTime('create_time', strtotime($times[0]), strtotime($times[1]));
            }

            if (isset($param['otherTimes']) && $param['otherTimes']) {
                foreach ($param['otherTimes'] as $k => $v) {
                    if (empty($v)) continue;
                    $decode = explode(' - ', $v);
                    $that->whereBetweenTime($k, strtotime($decode[0]), strtotime($decode[1]));
                }
            }

            foreach ($like as $v) {
                foreach ($v as $key => $vo) {
                    $that->whereLike($key, '%' . trim($vo) . '%');
                }
            }
            $order = explode(',', $order);
            if (!$export) {

                $list = $that->page(intval($page), intval($limit))->field($filed)->order($order[0], $order[1])->select();

                if (!empty($append)) {
                    $list->append($append);
                }
                return ['code' => 0, 'data' => $list, 'count' => getCount($that->fetchSql()->select())];
            } else {

                $list = $that->field($filed)->order($order[0], $order[1])->select();
                if (!empty($append)) {
                    $list->append($append);
                }
                return ['code' => 0, 'data' => $list];
            }

        } catch (\Exception $e) {
            dump($e);
            die;
            return [];
        }
    }

    public static function _save($param): array
    {
        try {
            if (isset($param['id']) && $param['id']) {
                $model = parent::update($param);
                $model->operation = 'save';
                $id = $param['id'];
            } else {
                $model = parent::create($param);
                $model->operation = 'create';
                $id = $model->id;
            }

            if(isset($param['is_push']) && $param['is_push'] == 1){
                $content = $param['describe'];
                $parameter = ['event_type' => 1004, 'sub_event_type' => 1101, 'id'=>$id];
                $extra = ['extras' => ['parameter' => json_encode($parameter)]];

                Jpush::sendAllNotify($content, $extra);
            }

            Event::trigger('Logs', $model);

            return ['code' => 1, 'msg' => '操作成功'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
